﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Rbac.Domain;
using Rbac.Repository.Admins;
using Rbac.Services.Admins;
using Rbac.Services.Admins.Dto;
using System;
using System.Collections.Generic;

namespace RbacSystem.Controllers
{
    [Route("/[controller]/[action]")]
    [ApiController]
    public class AdminController : ControllerBase
    {
        private readonly IAdminService adminService;

        /// <summary>
        /// 接口---抽象
        /// </summary>
        /// <param name="adminService"></param>
        public AdminController(IAdminService adminService)
        {
            this.adminService = adminService;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Create(CreateDto dto)
        {
            return Ok(adminService.Create(dto));
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("/Admin/GetAdmin")]
        public IActionResult Query(int Id)
        {
            return Ok(adminService.Query(Id));
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Update(EditDto editDto)
        {
            return Ok(adminService.Update(editDto));
        }

        [HttpGet]
        public IActionResult Delete(int Id)
        {
            return Ok(adminService.Delete(Id));
        }

        [HttpPost]
        public IActionResult ManyDelete(List<AdminDto> list)
        {
            return Ok(adminService.ManyDelete(list));
        }

        [HttpPost]
        public IActionResult BulkDelete(List<int> list)
        {
            return Ok(adminService.ManyDelete(list));
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Register(RegisterDto dto)
        {
            return Ok(adminService.Register(dto));
        }

        [HttpPost]
        public IActionResult Login(LoginDto dto)
        {
            return Ok(adminService.Login(dto));
        }

        [HttpGet("/Admin/Query")]
        public IActionResult Page([FromQuery]AdminQueryDto dto, int PageIndex = 1, int PageSize = 10)
        {
            var obj = adminService.Page(dto, PageIndex, PageSize);
            return Ok(new { 
             list = obj.Item1,
             totalCount = obj.Item2,
            });
        }

        [HttpGet]
        public IActionResult CheckUserName(string UserName,int AdminId = 0)
        {
            return Ok(adminService.CheckUserName(UserName, AdminId));
        }
    }
}
